<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/26
 * @Description: 朋友圈相关的
 */

namespace App\Http\Controllers\Api;

use App\Models\ImGroupUser;
use Illuminate\Http\Request;
use App\Models\ImFriendsCircle;
use App\Models\ImFriendsCircleComments;
use App\Models\ImFriendsCirclePraise;
use PhpParser\Node\Stmt\GroupUse;
use Symfony\Component\HttpFoundation\File\UploadedFile;


class FriendController
{

    /**
     * @desc 发布朋友圈
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 10:24
     */
    public function addCircle(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $headerList = $request->header();
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImFriendsCircleModel->addCircle($input, $userId, $headerList);
        if ($res['code'] == 0)
        {
            return okBack('添加成功');
        }
        else
        {
            return errBack('添加失败');
        }
    }

    /**
     * @desc 朋友圈动态列表
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 15:13
     */
    public function circleList(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImFriendsCircleModel->circleList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 发布朋友圈评论
     * @param Request $request
     * @param ImFriendsCircleComments $ImFriendsCircleCommentsModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 16:33
     */
    public function addComment(Request $request, ImFriendsCircleComments $ImFriendsCircleCommentsModel)
    {
        $input = $request->all();
        $user = $request->user();
        $res = $ImFriendsCircleCommentsModel->addComment($input, $user);
        if ($res['code'] == 0)
        {
            return okBack($res['msg'],  $res['data']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 朋友圈点赞
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 17:47
     */
    public function addCircleLike(Request $request, ImFriendsCirclePraise $ImFriendsCirclePraiseModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImFriendsCirclePraiseModel->addCircleLike($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 朋友圈详情
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 15:13
     */
    public function circleDetail(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $list = $ImFriendsCircleModel->circleDetail($input, $userId);

        return okBack($list);
    }

    /**
     * @desc 评论列表
     * @param Request $request
     * @param ImFriendsCircleComments $ImFriendsCircleCommentsModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 10:22
     */
    public function commentList(Request $request, ImFriendsCircleComments $ImFriendsCircleCommentsModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;

        $list = $ImFriendsCircleCommentsModel->commentList($input['fc_id'], $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 好友资料朋友圈主页
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 15:13
     */
    public function userCircleList(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $otherUid = (int) $request->query('other_uid', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImFriendsCircleModel->userCircleList($userId, $otherUid,  $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 自己朋友圈主页
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/26 15:13
     */
    public function myCircleList(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImFriendsCircleModel->myCircleList($userId,  $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 删除朋友圈
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/8 10:24
     */
    public function delCircle(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImFriendsCircleModel->delCircle($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['message']);
        }
        else
        {
            return errBack($res['message']);
        }
    }

    /**
     * @desc 朋友圈设为私密
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/8 10:24
     */
    public function setSecrecyCircle(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImFriendsCircleModel->setSecrecyCircle($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['message']);
        }
        else
        {
            return errBack($res['message']);
        }
    }

    /**
     * @desc 测试
     * @param Request $request
     * @param ImFriendsCircle $ImFriendsCircleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/8 10:24
     */
    public function groupAvatar(Request $request, ImFriendsCircle $ImFriendsCircleModel)
    {
        $groupUser = new ImGroupUser();
        $fileUrl = $groupUser->updateGroupHeading('169217797193729');

        echo "<pre>"; var_dump($fileUrl);exit;

        $file = $request->file($fileUrl);
        $resourceImg = radius_img('http://tangfen.oss-cn-hangzhou.aliyuncs.com/circle/20211207/2021120709355161aeba77a0a40.jpg');
        // //生成图片
        imagepng($resourceImg, "img/12345678.jpg");
        imagedestroy($resourceImg);
        echo "<pre>"; var_dump(234);exit;
        //   echo $resourceImg;exit;

        unlink("img/12345678.jpg");
        // $img = array(
        //     './img/123456.jpg',
        //     './img/1234567.jpg',
        //     './img/1234567.jpg',
        //     './img/1234567.jpg',
        // );
        // $a = getGroupAvatar($img,1,'./img/123345.jpg');
        // var_dump($a);exit;
    }


}
